﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using KerriganDataContract;
using KerriganDataContract.Forum;
using KerriganDataContract.Person;

namespace KerriganForum
{
    public partial class Topic : BasePage
    {
        private string Activity;
        private int iCount = 0;
        private string requestCookie;
        private int ID
        {
            get
            {
                if (ViewState["id"] == null)
                    ViewState["id"] = -1;
                return (int)ViewState["id"];
            }
            set
            {
                ViewState["id"] = value;
            }
        }


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!Page.IsPostBack)
            {

                using (ServiceProxy Srv = new ServiceProxy(Request.Cookies))
                {



                    try
                    {

                        requestCookie = Request.Cookies["BXPXAUTHFWCF"].Value;

                    }
                    catch (Exception ex)
                    {
                        requestCookie = null;
                    }

                    if (Identify.Anonymous == null || requestCookie == null || Identify.IdUser == 0)
                    {
                        Identify.CheckUser(requestCookie);
                    }
                    else if (requestCookie != null && Identify.Anonymous == true)
                    {
                        Identify.CheckUser(requestCookie);
                    }

                }


                ID = Convert.ToInt32(Request.QueryString["id"]);

                using (ServiceProxy Srv = new ServiceProxy())
                {

                    GridView1.DataSource = Srv.Service.GetTopicToDisplay(ID);
                    GridView1.DataBind();
                }

                buttonNoUseEdit.Click += new EventHandler(this.btnSearch_Click);
                btn_add_Click(sender, e);
   

               
            }


        }

        protected void GridView1_Paging(object sender, GridViewPageEventArgs e)
        {

            GridView1.PageIndex = e.NewPageIndex;
            RefreshDataGrid();
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {

            RefreshDataGrid();
        }

        protected void btn_add_Click(object sender, EventArgs e)
        {
            Activity = "AddTopic";
            buttonNoUse.Click += new EventHandler(this.btnSearch_Click);
            btnAddTopic.Attributes.Add("onClick", "ShowNewModal();");
        }


        protected void RefreshDataGrid()
        {
            this.GridView1.DataSource = null;
            using (ServiceProxy Srv = new ServiceProxy())
            {

                GridView1.DataSource = Srv.Service.GetTopicToDisplay(ID);
                GridView1.DataBind();

                buttonNoUseEdit.Click += new EventHandler(this.btnSearch_Click);

            }

        }



        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                
                using (ServiceProxy Srv = new ServiceProxy())
                {
                    vwGetForumTopic topic = e.Row.DataItem as vwGetForumTopic;

                    if (Identify.Anonymous != true)
                    {

                        if (topic != null)
                        {

                            if (topic.Owner == Identify.IdUser || Identify.TypeUser == 1)
                            {

                                (e.Row.FindControl("lnkEdit") as LinkButton).Visible = true;
                                (e.Row.FindControl("lnkDelete") as LinkButton).Visible = true;
                                btnAddTopic.Visible = true;
                                (e.Row.FindControl("lnkEdit") as LinkButton).Attributes.Add("onClick", "ShowEditModal('" + topic.IdForumTopic + "');");
                                (e.Row.FindControl("lnkDelete") as LinkButton).CommandArgument = topic.IdForumTopic.ToString();

                            }
                            else
                            {
                                (e.Row.FindControl("lnkEdit") as LinkButton).Visible = false;
                                (e.Row.FindControl("lnkDelete") as LinkButton).Visible = false;
                                btnAddTopic.Visible = true;

                            }
                        }

                    }
                    else {

                        (e.Row.FindControl("lnkEdit") as LinkButton).Visible = false;
                        (e.Row.FindControl("lnkDelete") as LinkButton).Visible = false;
                        btnAddTopic.Visible = false;
                    }
                    
                }
                iCount++;
            }
            
        }


        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "MyDelete")
            {
                using (ServiceProxy Srv = new ServiceProxy())
                {
                    vwGetForumTopicByPerson topic = e.CommandSource as vwGetForumTopicByPerson;
                    int id = Convert.ToInt32(e.CommandArgument);
                    Srv.Service.DeleteTopic(id);
                    RefreshDataGrid();
                }
            }
        }

        protected void btnOkay_Click(object sender, EventArgs e)
        {


            try
            {
                    AddTopic();
                    ClientScript.RegisterStartupScript(this.GetType(), "onload", "onSuccess();", true);
                    RefreshDataGrid();
                    txtNameTopic.Text = "";
                    TxtMessage.InnerText = "";
            }
            catch (Exception ex)
            {
                ClientScript.RegisterStartupScript(this.GetType(), "onload", "onError();", true);
            }
        }



        private void AddTopic()
        {
            using (ServiceProxy Srv = new ServiceProxy())
            {
                ForumMessageDC bo = new ForumMessageDC();
                bo.Topic = new ForumTopicDC();
                bo.Topic.Title = txtNameTopic.Text;
                bo.Message = TxtMessage.InnerText;
                bo.Person = new PersonDC();
                bo.Person.IdPerson = Identify.IdUser;
                bo.Topic.ForumCategory = new ForumCategoryDC();
                bo.Topic.ForumCategory.IdForumCategory = ID;
                Srv.Service.AddTopicAndMessage(bo);


            }

        }


        [System.Web.Services.WebMethod()]
        [System.Web.Script.Services.ScriptMethod]
        public void RefreshDataGrid2()
        {
            
            this.GridView1.DataSource = null;
            using (ServiceProxy Srv = new ServiceProxy())
            {

                GridView1.DataSource = Srv.Service.GetTopicToDisplay(ID);
                GridView1.DataBind();

                //buttonNoUse.Click += new EventHandler(this.btnSearch_Click);
                buttonNoUseEdit.Click += new EventHandler(this.btnSearch_Click);

            }

        }

    }
}